package my.state.headfirst.low;

import lombok.ToString;

//糖果机
@ToString
public class GunballMachine {

    //售罄状态
    public final static int SOLD_OUT = 0;
    //没有投币状态
    public final static int NO_QUARTER = 1;
    //投硬币状态
    public final static int HAS_QUARTER = 2;
    //可销售状态
    public final static int SOLD = 3;


    //糖果机状态
    private int state = SOLD_OUT;
    //糖果机糖果数量
    private int count = 0;

    public GunballMachine(int count){
        this.count = count;
        if (count>0){
            state = NO_QUARTER;
        }
    }

    //-------------用户动作-------------

    //投硬币
    public void insertQuarter() {
        if (state == HAS_QUARTER) {
            System.out.println("亲，已经投过了硬币,准备出出糖果吧！");
        } else if (state == NO_QUARTER) {
            state = HAS_QUARTER;
            System.out.println("亲，收到一枚硬币");
        } else if (state == SOLD_OUT) {
            System.out.println("太火爆，已经被抢光了！");
        } else if (state == SOLD) {
            System.out.println("请先出糖果吧！");
        }
    }

    //退钱
    public void ejectQuarter() {
        if (state == HAS_QUARTER) {
            System.out.println("亲，退币成功！");
            state = NO_QUARTER;
        } else if (state == NO_QUARTER) {
            System.out.println("亲，还没有投硬币呀！");
        } else if (state == SOLD_OUT) {
            System.out.println("亲，已经售罄了,不能投币，当然不能退币！");
        } else if (state == SOLD) {
            System.out.println("亲，已经出过糖果了，不能退币呀！");
        }
    }

    //晃动摇杆，准备发糖果
    public void turnCrank() {
        if (state == HAS_QUARTER) {
            System.out.println("亲，稍定马上了出糖果请稍后");
            state = SOLD;
            dispense();
        } else if (state == NO_QUARTER) {
            System.out.println("亲，还没有投硬币呀！");
        } else if (state == SOLD_OUT) {
            System.out.println("亲，已经售罄了,不能出糖果了！");
        } else if (state == SOLD) {
            System.out.println("亲，别想骗过糖果机出两次糖果");
        }
    }


    //内部动作： 发糖果
    private void dispense() {
        if (state == HAS_QUARTER) {
            System.out.println("亲，请先转动摇杆");
        } else if (state == NO_QUARTER) {
            System.out.println("亲，还没有投硬币呀！");
        } else if (state == SOLD_OUT) {
            System.out.println("亲，已经售罄了");
        } else if (state == SOLD) {
            count--;
            if (count == 0) {
                state = SOLD_OUT;
            } else {
                System.out.println("亲，出糖果成功");
                state = NO_QUARTER;
            }
        }
    }

}
